package t2h.listsong;

import javax.microedition.lcdui.Screen;

import t2h.activities.*;
import t2h.gui.SearchScreen;
import t2h.gui.SongDetail;
import t2h.gui.T2HKaraoke;
import t2h.valueobjects.SongVO;

import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.*;
import com.sun.lwuit.plaf.UIManager;

public class ListView extends Form{
	public static final int MAX_ITEMS = 1000;
    public final String LIST_ITEM_TEXT =
            Compatibility.toLowerCaseIfFT("Id ");
    public static final String LONG_TEXT =
            ", Lorem ipsum dolor sit amet, consectetur adipiscing "
            + "elit. Phasellus vel neque pulvinar libero vestibulum malesua.";
    protected List list;
    protected int[] originalSelectedIndices; // used when abandoning changes
    //private boolean confirmationQuery = false;
    
	public ListView() {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        this.addShowListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                originalSelectedIndices = getSelectedIndices();
            }
        });
        
        //Button BACK
        /*Command backCommand = new Command("Back") {
            public void actionPerformed(ActionEvent e) {
                back();
            }
        };
        setBackCommand(backCommand);*/
    }
	
	public void showSelectedDialog(SongVO songvo, final Form detailForm) {		
		//final Form detailForm = new Form("Song Detail");
		Label lb = new Label(songvo.getId() +"-"+ songvo.getAuthor());
		Label txta = new Label(songvo.getLyric());
	 //pa = new Container(songvo.getLyric());
		txta.setHeight(300);
		detailForm.addComponent(lb);
		detailForm.addComponent(txta);
		
		//detailForm.show();
		Command c = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
            	//detailForm.replace(detailForm , T2HKaraoke.getMainForm(),UIManager.getInstance().getLookAndFeel().getDefaultFormTransitionOut().copy(true));

            	SearchScreen s = new SearchScreen();
            	s.executeDemo(detailForm);
            	detailForm.show();
            	//T2HKaraoke.getMainForm().show();
            }
        };
        detailForm.addCommand(c);
        detailForm.setBackCommand(c);
        detailForm.show();
    }
	 Form detailForm;
	public void showSelectedDialog(SongVO songvo) {	
		
		detailForm = new Form("Song Detail");
		detailForm.setScrollable(true);
		TextArea txtId = new TextArea(songvo.getId(), 1, 10);
		txtId.setSingleLineTextArea(false);
		txtId.setEditable(false);
		//txtId.setMaxSize(255);
		TextArea txtName = new TextArea(songvo.getName(), 2, 10);
		txtName.setSingleLineTextArea(false);
		txtName.setEditable(false);
		//txtName.setMaxSize(255);
		TextArea txtAuthor = new TextArea(songvo.getAuthor(), 2, 10);
		txtAuthor.setSingleLineTextArea(false);
		txtAuthor.setEditable(false);
		//txtAuthor.setMaxSize(255);
		Label lbLyric = new Label("Lời bài hát: ");
		TextArea txtLyric = new TextArea(songvo.getLyric(), 70, 10);
		txtLyric.setSingleLineTextArea(false);
		txtLyric.setEditable(false);
		detailForm.addComponent(createPair("Mã số:   ", txtId, 30));		
		detailForm.addComponent(createPair("Bài hát: ", txtName, 30));		
		detailForm.addComponent(createPair("Tác giả: ", txtAuthor, 30));
		detailForm.addComponent(lbLyric);
		detailForm.addComponent(txtLyric);
		//txt.setMaxSize(255);
		//txt.setScrollAnimationSpeed(MAX_ITEMS);
		//txt.setText();
		//txt.setScrollVisible(true);
		//pa = new Container(songvo.getLyric());
		//txta.setHeight(300);
		//detailForm.addComponent(lb);
		//detailForm.addComponent(createPair(txt, lb, 20, 10));
		//detailForm.addComponent(lb1);
		//detailForm.addComponent(txtId);
		//detailForm.addComponent(lb2);
		//detailForm.addComponent(txtName);
		//detailForm.addComponent(lb3);
		//detailForm.addComponent(txtAuthor);		
		//detailForm.show();
		
		Command c = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
            	//detailForm.replace(detailForm , T2HKaraoke.getMainForm(),UIManager.getInstance().getLookAndFeel().getDefaultFormTransitionOut().copy(true));
            	detailForm = new Form("Search song");
            	SearchScreen s = new SearchScreen();
            	s.executeDemo(detailForm);
            	detailForm.show();
            	//T2HKaraoke.getMainForm().show();
            }
        };
        detailForm.addCommand(c);
        detailForm.setBackCommand(c);
        detailForm.show();
		
		/*Command okCommand = new Command("OK");
        String text = (getSelectedIndices().length == 0 ? "No selected items." : "");
        for (int i = 0; i < getSelectedIndices().length; i++) {
        	text += songvo.getLyric();
        }
        Dialog dialog = new Dialog();
        dialog.setScrollable(true);

        dialog.show(songvo.getId()+ " - " + songvo.getName(),
                text,
                null,
                null,
                Dialog.TYPE_NONE,
                null,
                15000L);*/
        
		
        

       /*Dialog.show(songvo.getId(),
                text,
                okCommand,
                new Command[] {okCommand},
                Dialog.TYPE_INFO,
                null,
                15000L);*/
		
		
		
        //back();
        //T2HKaraoke.getMainForm().showBack();
        //SearchScreen
    }
	
	protected Container createPair(String label, Component c, int minWidth) {
        Container pair;
        Label l =  new Label(label);
        Dimension d = l.getPreferredSize();
        d.setWidth(Math.max(d.getWidth(), minWidth));
        l.setPreferredSize(d);
        c.setLabelForComponent(l);
        if(UIManager.getInstance().isThemeConstant("ComponentGroupBool", false)) {
            pair = new Container(new BoxLayout(BoxLayout.Y_AXIS));
            pair.addComponent(l);
            l.setUIID("TitleLabel");
            ComponentGroup g = new ComponentGroup();
            g.addComponent(c);
            pair.addComponent(g);
        } else {
            pair = new Container(new BorderLayout());
            pair.addComponent(BorderLayout.WEST,l);
            pair.addComponent(BorderLayout.CENTER, c);
        }
        return pair;
    }
	
	public int[] getSelectedIndices() {
        return new int[]{list.getSelectedIndex()};
    }
	
	public void setSelectedIndices(int[] indices) {
        if (indices.length > 0) {
            list.setSelectedIndex(indices[0]);
        }
    }
	
	/*public void setShowConfirmationQuery(boolean value) {
        confirmationQuery = value;
    }
	
	public boolean hasChanged() {
        int[] s = getSelectedIndices();
        if (s.length != originalSelectedIndices.length) {
            return true;
        }
        for (int i = 0; i < s.length; i++) {
            if (s[i] != originalSelectedIndices[i]) {
                return true;
            }
        }
        return false;
    }*/
     
    
    /*protected void back() {
    	T2HKaraoke.getMainForm().show();
        if (hasChanged() && confirmationQuery) {
            int[] tmp = new int[originalSelectedIndices.length];
            System.arraycopy(originalSelectedIndices, 0, tmp, 0, tmp.length);
            if (Dialog.show("save changes", "Save changes made?", "Yes", "No")) {
                showSelectedDialog(); // Proceed with the changes
            } else {
                setSelectedIndices(tmp); // Abandon changes
                T2HKaraoke.getMainForm().show();
            }
        } else { 
        	T2HKaraoke.getMainForm().show();
        }
    }*/

    public void setTitle(String title) {
        super.setTitle(Compatibility.toLowerCaseIfFT(title));
    }

    public String toString() {
        return getTitle();
    }
}
